$(function () {
    // findAll();
})

let curPage = 1;
let pageSize = 10;
layui.use(['laydate', 'form','laypage', 'layer'], function () {
        var laypage = layui.laypage
            ,layer = layui.layer;
        var laydate = layui.laydate;
        var form = layui.form;
        // 监听全选

        pages();
        form.on('submit(sreach)',
            function (data) {
                pages();
                return false;
            });
        form.on('checkbox(checkall)', function (data) {

            if (data.elem.checked) {
                $('tbody input').prop('checked', true);
            } else {
                $('tbody input').prop('checked', false);
            }
            form.render('checkbox');
        });

        laydate.render(
            { elem:'#start'}
        );

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });
    }
)

function getCount() {
    let count = 0;
    $.ajax({
            'url': '/back/user/count',
            data: getSearchData(),
            async: false,
            type: 'get',
            dataType: 'json',
            success(res) {
                if(res.result=="notLogin"){
                    window.open("/html/login.html", "_top");
                }
                console.log(res)
                count = res.count
                console.log(count)
                return count;
            }
        }
    )
    return count;
}
function findAll() {
    $.ajax({
            url: '/back/user/findAll',
            data:getSearchData(),
            type:'get',
            dataType:'json',
            success(res){
                if(res.result=="notLogin"){
                    window.open("/html/login.html", "_top");
                }
                setList(res.list)
            }
        }
    )

}

function setList(data) {


    let html ='';

    if (data.length==0){
        $("#tbody").html(html)
        layui.use('layer',function () {
            let layer = layui.layer;
            layer.alert('没有查询到内容');
        })
        return;
    }
    for(let i=0 ;i< data.length;i++){

        html += '<tr>\n' +
            '                            <td>' + data[i].id + '</td>\n' +
            '                            <td>' + data[i].username + '</td>\n' +
            '                            <td>' + setUndefinedNull(data[i].gender) + '</td>\n' +
            '                            <td>' + setUndefinedNull(data[i].mobile) + '</td>\n' +
            '                            <td>' + setUndefinedNull(data[i].birthday) + '</td>\n' +
            '                            <td>' + setUndefinedNull(data[i].hobby) + '</td>\n' +
            '                            <td class="td-status">\n' +
            '                                <span class="layui-btn layui-btn-normal layui-btn-mini">' + (data[i].enable == 1? "已启用" : "已停用") + '</span></td>\n' +
            '                            <td class="td-manage">\n' +
            '                                <a onclick="member_stop1(this,' + data[i].id + ')" href="javascript:;" title="' + (data[i].enable == 1 ? "启用" : "锁定") + '">\n' +
            '                                    <i class="layui-icon">&#xe601;</i>\n' +
            '                                </a>\n' +
            '                                <a title="编辑" onclick="edit(' + data[i].id + ')"\n' +
            '                                   href="javascript:;">\n' +
            '                                    <i class="iconfont">&#xe642;</i>\n' +
            '                                </a>\n' +
            '                                <a onclick="xadmin.open(\'修改密码\',\'member-password.html\',600,400)" title="修改密码"\n' +
            '                                   href="javascript:;">\n' +
            '                                    <i class="layui-icon">&#xe631;</i>\n' +
            '                                </a>\n' +
            '                                <a title="删除" onclick="member_del1(this,' + data[i].id + ')" href="javascript:;">\n' +
            '                                    <i class="layui-icon">&#xe640;</i>\n' +
            '                                </a>\n' +
            '                            </td>\n' +
            '                        </tr>';
    }
    $("#tbody").html(html)

    let obj= $("#tbody").find(".td-status").find('span')

    for(let i =0 ;i<obj.length;i++){



        if(obj[i].innerHTML=="已停用"){
            obj[i].className="layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled";
        }
    }
}

function setUndefinedNull(data) {
    return data == null?'':data;

}

function getSearchData() {
    let data = {
        loginName:sessionStorage.getItem("loginName"),
        username: $("#username").val(),
        start: $("#start").val(),
        end: $("#end").val(),
        mobile: $("#phone").val(),
        gender: $("#gender").val(),
        hobby: getHobby().toLocaleString(),
        curPage: curPage,
        pageSize: pageSize
    };
    return data;
}

function edit(id) {
    sessionStorage.setItem("userId", id);
    xadmin.open('编辑',"/html/user/member-edit.html",600, 400)

}

function member_del1(obj,id) {


    layer.confirm('确认要删除吗？', function (index) {

        //发异步删除数据
        $.ajax({
            url: '/back/user/del',//地址：访问后台的地址
            data: {id: id,
                loginName:sessionStorage.getItem("loginName")},//前端传递给后台的数据
            type: 'post',
            dataType: 'json',//是后端传递到前端的数据格式
            success: function (data) {//后端给前端的一个反馈
                if(data.result=="notLogin"){
                    window.open("/html/login.html", "_top");
                }
                if (data.result == "success") {
                    $(obj).parents("tr").remove();
                    layer.msg('已删除!', {icon: 1, time: 1000});
                }
            }

        });

    });
}

function getHobby() {
    var value = [];
    $('input[name=hobby]:checked').each(function () {
        //这里用所选复选框或运算后的值
        value.push($(this).val());
    });
    return value;
}

/*用户-停用*/
function member_stop1(obj,id){

    layer.confirm('确认要停用吗？',function(index){

        $.ajax(
            {
                url: '/back/user/changeEnable',//地址：访问后台的地址
                data: {id: id,
                    loginName:sessionStorage.getItem("loginName")},//前端传递给后台的数据
                type: 'post',
                dataType: 'json',//是后端传递到前端的数据格式
                success: function (data) {
                    if(data.result=="notLogin"){
                        window.open("/html/login.html", "_top");
                    }
                    if($(obj).attr('title')=='启用'){

                        //发异步把用户状态进行更改
                        $(obj).attr('title','停用')
                        $(obj).find('i').html('&#xe62f;');

                        $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                        layer.msg('已停用',{icon: 5,time:1000});

                    }else{
                        $(obj).attr('title','启用')
                        $(obj).find('i').html('&#xe601;');

                        $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                        layer.msg('已启用',{icon: 5,time:1000});
                    }

                }
            }
        )



    });
}
function pages() {
    layui.use('laypage', function () {
        var laypage = layui.laypage;
        laypage.render({
            elem: 'page-define'
            ,count: getCount()
            ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            ,jump: function(obj){

                curPage = obj.curr;
                pageSize = obj.limit;
                findAll();
                console.log(obj)
            }
        });

    })

}

/*
<tr>
<td>
<input type="checkbox" name="id" value="1"   lay-skin="primary">
    </td>
    <td>1</td>
    <td>小明</td>
    <td>男</td>
    <td>13000000000</td>
    <td>北京市 海淀区</td>
<td class="td-status">
    <span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span></td>
<td class="td-manage">
    <a onclick="member_stop(this,'10001')" href="javascript:;"  title="启用">
    <i class="layui-icon">&#xe601;</i>
</a>
<a title="编辑"  onclick="xadmin.open('编辑','member-edit.html',600,400)" href="javascript:;">
    <i class="layui-icon">&#xe642;</i>
</a>
<a onclick="xadmin.open('修改密码','member-password.html',600,400)" title="修改密码" href="javascript:;">
    <i class="layui-icon">&#xe631;</i>
</a>
<a title="删除" onclick="member_del(this,'要删除的id')" href="javascript:;">
    <i class="layui-icon">&#xe640;</i>
</a>
</td>
</tr>*/
